package com.ht.module.info.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.abnormal.HtException;
import com.ht.module.bus.entity.BusDict;
import com.ht.module.bus.entity.BusFile;
import com.ht.module.bus.service.IBusDictService;
import com.ht.module.bus.service.IBusFileService;
import com.ht.module.info.entity.InfoGoods;
import com.ht.module.info.entity.InfoMerchants;
import com.ht.module.info.mapper.InfoGoodsMapper;
import com.ht.module.info.mapper.InfoMerchantsMapper;
import com.ht.module.info.service.IInfoGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.info.service.IInfoMerchantsService;
import com.ht.module.sys.entity.SysUser;
import com.ht.module.vo.MaterialNumVo;
import com.ht.module.vo.SortVo;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import com.ht.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-03
 */
@Service
public class InfoGoodsServiceImpl extends ServiceImpl<InfoGoodsMapper, InfoGoods> implements IInfoGoodsService {
    @Autowired
    private IInfoMerchantsService infoMerchantsService;
    @Resource
    private InfoMerchantsMapper infoMerchantsMapper;
    @Autowired
    private IBusDictService busDictService;
    @Autowired
    private IBusFileService busFileService;

    @Override
    public void add(InfoGoods infoGoods) {
        //获取商户
        InfoMerchants infoMerchants = infoMerchantsService.getOne(new QueryWrapper<InfoMerchants>().eq("user_id", UserUtil.getUserId()));
        if(ObjectUtil.isEmpty(infoMerchants)){
            throw new HtException("您未在本平台注册商户，不可发布商品！");
        }
        CommMethod.beanCreate(infoGoods);
        infoGoods.setMerchantsId(infoMerchants.getId());
        baseMapper.insert(infoGoods);
        //将选中的材料插入业务字典关联表
        for (MaterialNumVo vo : infoGoods.getMaterialList()) {
            BusDict busDict=new BusDict();
            CommMethod.beanCreate(busDict);
            busDict.setCode("material")
                    .setValue(vo.getVal())
                    .setExtend(vo.getNum())
                    .setTableId(infoGoods.getId());
            busDictService.save(busDict);
        }

    }

    @Override
    public Pager<InfoGoods> findPage(Pager<InfoGoods> pager, InfoGoods en) {
        QueryWrapper<InfoGoods> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(en.getName())){
            qw.like("name",en.getName());
        }
        if(StrUtil.isNotEmpty(en.getCategory())){
            qw.eq("category",en.getCategory());
        }
        if(StrUtil.isNotEmpty(en.getState())){
            qw.eq("state",en.getState());
        }
        qw.eq("create_user",UserUtil.getUserId());
        baseMapper.selectPage(pager,qw);
        //查询出关联材料
        for (InfoGoods record : pager.getRecords()) {
            List<BusDict> list = busDictService.list(
                    new QueryWrapper<BusDict>()
                            .eq("code", "material")
                            .eq("table_id", record.getId())
            );
            record.setMaterialList(list.stream().map(x->{
                MaterialNumVo vo = new MaterialNumVo();
                vo.setVal(x.getValue());
                vo.setNum(x.getExtend());
                return vo;
            }).collect(Collectors.toList()));
        }


        return pager;
    }

    @Override
    public void updateState(String id, String val) {
        InfoGoods info=new InfoGoods();
        info.setId(id);
        info.setState(val);
        CommMethod.beanUpdate(info);
        baseMapper.updateById(info);
    }

    @Override
    public void editById(InfoGoods infoGoods) {
        //获取当前商品
        InfoGoods temp = baseMapper.selectById(infoGoods.getId());
        if(ObjectUtil.isNull(temp)){
            throw new HtException("商品不存在");
        }
        if(!StrUtil.equals(temp.getCreateUser(),UserUtil.getUserId())){
            throw new HtException("没有权限修改该商品!");
        }

        baseMapper.updateById(infoGoods);
        //删除原有的
        busDictService.remove(
                new QueryWrapper<BusDict>()
                    .eq("code","material")
                    .eq("table_id",temp.getId())
        );

        //将选中的材料插入业务字典关联表
        for (MaterialNumVo vo : infoGoods.getMaterialList()) {
            BusDict busDict=new BusDict();
            CommMethod.beanCreate(busDict);
            busDict.setCode("material")
                    .setValue(vo.getVal())
                    .setExtend(vo.getNum())
                    .setTableId(infoGoods.getId());
            busDictService.save(busDict);
        }
    }

    @Override
    public Pager<InfoGoods> notLoginPage(Pager<InfoGoods> pager, InfoGoods en) {
        QueryWrapper<InfoGoods> qw=new QueryWrapper<>();
        //区划
        if(StrUtil.isNotEmpty(en.getInCity())){
            qw.eq("tab.in_city",en.getInCity());
        }
        //作用
        if(StrUtil.isNotEmpty(en.getUse())){
            qw.eq("tab.use",en.getUse());
        }
        //类别
        if(StrUtil.isNotEmpty(en.getCategory())){
            qw.eq("tab.category",en.getCategory());
        }

        //价格区间
        if(ObjectUtil.isNotEmpty(en.getPriceVo())){
            qw.ge("tab.price",en.getPriceVo().getMin());
            qw.lt("tab.price",en.getPriceVo().getMax());
        }

        //枝数区间
        if(ObjectUtil.isNotEmpty(en.getNumberVo())){
            qw.ge("tab.number",en.getNumberVo().getMin());
            qw.lt("tab.number",en.getNumberVo().getMax());
        }
        //材料
        if(ObjectUtil.isNotEmpty(en.getMateria())){
            qw.like("tab.material_str",en.getMateria());
        }

        //排序
        if(ObjectUtil.isNotEmpty(en.getSort())){
            switch (en.getSort().getVal()){
                case "01":
                    break;
                case "02":
                    //销量
                    qw.orderBy(true,!en.getSort().getDesc(),"sales");
                    break;
                case "03":
                    //价格
                    qw.orderBy(true,!en.getSort().getDesc(),"price");
                    break;
                case "04":
                    qw.orderBy(true,!en.getSort().getDesc(),"create_date");
                    //最新
                    break;
            }
        }
        return baseMapper.findPage(pager,qw);
    }

    @Override
    public InfoGoods findById(String id) {
        InfoGoods infoGoods = baseMapper.selectById(id);
        if(ObjectUtil.isNotNull(infoGoods)){
            //设置材料
            infoGoods.setBusFiles(busFileService.list(
                    new QueryWrapper<BusFile>()
                            .eq("table_id",id)
            ));
            //设置字典
            infoGoods.setBusDicts(busDictService.list(
                    new QueryWrapper<BusDict>()
                            .eq("table_id",id)
            ));
            //设置店铺信息
            infoGoods.setInfoMerchants(infoMerchantsMapper.findById(infoGoods.getMerchantsId()));
        }

        return infoGoods;
    }
}
